package com.demo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class RemoveFromCart
 */
@WebServlet("/RemoveFromCart")
public class RemoveFromCart extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RemoveFromCart() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter pw=response.getWriter();

		String productIdArray[]=request.getParameterValues("productId");
		HttpSession session=request.getSession();
		
		Map<Integer, Integer> cartInfo=(Map<Integer, Integer>) session.getAttribute("cartInfo");
		System.out.println(cartInfo.size());
        System.out.println(cartInfo);
		 if(productIdArray.length>0){
		    	
		    	for (String productId : productIdArray) {
                 
		    		    int productIdInt=Integer.parseInt(productId);
		    		    if(cartInfo.containsKey(productIdInt)){
		    		    	
		    		    	System.out.println(cartInfo.remove(productIdInt));
		    		    	
		    		    }else {
		    		   	
		    				 pw.println("No item is selected for removal");
		    			 
		    		    }
				}
		    	session.setAttribute("cartInfo", cartInfo);
		 }
		 response.sendRedirect("ViewCartInfo");
	}
}
			

	
		

	


